package wfmanage.action;

import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;


import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.util.DB;
import com.util.MyLog;

import edu.wfmanage.models.Lookup;

public class AdminAssignRules extends ActionSupport{
	private String domain;
	private String display="";
	public String getDisplay() {
		return display;
	}
	public void setDisplay(String display) {
		this.display = display;
	}
	private String selectedDomain="";
	public String getSelectedDomain() {
		return selectedDomain;
	}
	public void setSelectedDomain(String selectedDomain) {
		this.selectedDomain = selectedDomain;
	}
	private String domain_name="";
	private String commandButton = "";
	private ArrayList<Lookup> WFPoints = new ArrayList<Lookup>();
	private ArrayList<Lookup> domain_WFPoints = new ArrayList<Lookup>();
	private ArrayList<String> domainList = new ArrayList<String>();
	public ArrayList<String> getDomainList() {
		return domainList;
	}
	public void setDomainList(ArrayList<String> domainList) {
		this.domainList = domainList;
	}
	public ArrayList<String> getWFList() {
		return WFList;
	}
	public void setWFList(ArrayList<String> wFList) {
		WFList = wFList;
	}
	private ArrayList<String> WFList = new ArrayList<String>();
	int domain_id=1;
	int fromWorkflowPoint_id=1;
	int toWorkflowPoint_id=1;
	int initialWorkflowPoint_id=1;
	private String msg="";
	public String getMsg() {
		return msg;
	}
	public void setMsg(String msg) {
		this.msg = msg;
	}
	//private String 
	public String execute() throws Exception {
		 MyLog.log("Inside AdminAssignRules : Method : execute");
		 
		 Map session = ActionContext.getContext().getSession();
		 domainList=(ArrayList)session.get("domainList");
		 MyLog.log("size"+domainList.size());
		 for (int i = 0; i < domainList.size(); i++){
	        	String list = domainList.get(i);
	           	MyLog.log("name:"+list);
	        }

	        HttpServletRequest request=(HttpServletRequest)ActionContext.getContext().get(ServletActionContext.HTTP_REQUEST);
	 
	        Enumeration paramNames= request.getParameterNames();
	 
	        while(paramNames.hasMoreElements())
	        {
	            String param=(String)paramNames.nextElement();
	            MyLog.log("\nParameter Name : "+param);
	            MyLog.log("Parameter Values : ");
	             if(param.equals("myDomain")){
	            	 String[] paramValues=request.getParameterValues(param);
		             
	 	            for(String paramVal:paramValues)
	 	            {
	 	            	Lookup domain=Lookup.findOne("domain", "where domain_name = '"+paramVal+"'");
	 	            	domain_id=domain.getId();
	 	            	domain_name=domain.getName();
	 	            	selectedDomain=domain_name;
	 	                MyLog.log(paramVal);
	 	            }
	             }
	             else if(param.equals("FromWFPoint")){
	            	 String[] paramValues=request.getParameterValues(param);
		             
	 	            for(String paramVal:paramValues)
	 	            {
	 	            	Lookup fromWF=Lookup.findOne("WorkflowPoint", " where WorkflowPoint_name = '"+paramVal+"'");
	 	            	fromWorkflowPoint_id = fromWF.getId();
	 	            }
	             }
	             else if(param.equals("ToWFPoint")){
	            	 String[] paramValues=request.getParameterValues(param);
		             
	 	            for(String paramVal:paramValues)
	 	            {
	 	            	Lookup toWF=Lookup.findOne("WorkflowPoint", " where WorkflowPoint_name = '"+paramVal+"'");
	 	            	toWorkflowPoint_id = toWF.getId();
	 	            }
	             }
	             else if(param.equals("initialWFPoint")){
	            	 String[] paramValues=request.getParameterValues(param);
		             
	 	            for(String paramVal:paramValues)
	 	            {
	 	            	Lookup initialWF=Lookup.findOne("WorkflowPoint", " where WorkflowPoint_name = '"+paramVal+"'");
	 	            	initialWorkflowPoint_id = initialWF.getId();
	 	            }
	             }
	        }
	        if (this.commandButton.startsWith("Display")) {
	        	this.domain_WFPoints=Lookup.findWF(""+domain_id);
	        	MyLog.log("domain_id="+domain_id);
				for (int i = 0; i < domain_WFPoints.size(); i++){
		        	Lookup list = domain_WFPoints.get(i);
		        	WFList.add(list.getName());
		        	MyLog.log("name:"+list.getName());
		        }
				display="display";
				MyLog.log("selected:"+selectedDomain);
				
				return "assignRules";
	        	
	        }
	        else if (this.commandButton.startsWith("Save")) {
	        	MyLog.log("domain_id="+domain_id);
	        	MyLog.log("selected domain"+selectedDomain);
	        		int result=insert(fromWorkflowPoint_id,toWorkflowPoint_id);
	           		int res=insertInitialWF(initialWorkflowPoint_id);
	           		 if(result>0 && res>0){
	           			 msg="Added Progress Rules Successfully!!";
	           			 MyLog.log("Added Progress Rules Successfully!!");
	           			return "displayWF";
	           		 }
	           		 else{
	           			 msg="Cannot Add, Progress Rules";
	           			 MyLog.log("Cannot Add, Progress Rules");
	           			 return "displayWF";
	           		 }
           	 	//}
           	 //return "displayWF";
            }
            else if (this.commandButton.startsWith("Back to Admin")) {
      			return "admin_home";
      		}
	
	    	return "error";
	}
	public String getCommandButton() {
		return commandButton;
	}
	public void setCommandButton(String commandButton) {
		this.commandButton = commandButton;
	}
	public int insert(int fromWorkflowPoint_id,int toWorkflowPoint_id) {
		String insertSQL = "insert into progress_workflow(fromWorkflowPoint_id,toWorkflowPoint_id) values" +
				"				("+fromWorkflowPoint_id +", "+toWorkflowPoint_id+");";
		MyLog.log("insert query"+insertSQL);
		return DB.update(insertSQL);
	}
	public int insertInitialWF(int initialWorkflowPoint_id) {
		String insertSQL = "update DOMAIN_WORKFLOWPOINT set initialworkflowpoint=1 " +
				"where workflowpoint_id= "+initialWorkflowPoint_id+";";
		MyLog.log("insert query"+insertSQL);
		return DB.update(insertSQL);
	}
}
